package com.asa.chart.work;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.asa.chart.base.Line;
import com.asa.chart.base.Point;


public class AlgoFrame extends JFrame{
	
	public AlgoFrame(String title,int canvasWidth,int canvasHeight) {
		super(title);
		this.canvasWidth = canvasWidth;
		this.canvasHeight = canvasHeight;
		
		AlgoCanvas canvas = new AlgoCanvas();
//		canvas.setPreferredSize(new Dimension(canvasWidth,canvasHeight));
		
//		this.setSize(canvasWidth,canvasHeight);
		this.setContentPane(canvas);
		pack();
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
	}
	public int getCanvasWidth() {
		return canvasWidth;
	}
	public int getCanvasHeight() {
		return canvasHeight;
	}
	private int canvasWidth;
	private int canvasHeight;

	private List<Line> mapline;
	private List<List<Line>> canves;
	private List<String> text;
	private List<Point> textpoint;
	
	
	public void render( List<Line> mapline, List<List<Line>> canves,List<String> text, List<Point> textpoint) {
		
	
		this.mapline = mapline;
		this.canves = canves;
		
		this.text = text;
		this.textpoint = textpoint;
		
		
		this.repaint();
	}
	
	private class AlgoCanvas extends JPanel{
		
		@Override
		public void paintComponent(Graphics g) {
			
			super.paintComponent(g);
			
			
			Graphics2D g2d = (Graphics2D) g;
			AlgoVisHelper.setRenderingHints(g2d);

			AlgoVisHelper.setColor(g2d,Color.GRAY);
			if (mapline!=null) {
				for (int i = 0; i < mapline.size(); i++) {
					Line line = mapline.get(i);
					int x1 = line.spoint.x;
					int y1 = line.spoint.y;
					int x2 = line.epoint.x;
					int y2 = line.epoint.y;
					AlgoVisHelper.drawLine(g2d, x1, y1, x2, y2);
				
				}
			}
			
			if (canves!=null) {
				for (int i = 0; i < canves.size(); i++) {
					List<Line> canve = canves.get(i);
					for (int j = 0; j < canve.size(); j++) {
						
						Line line = canve.get(j);

						int x1 = line.spoint.x;
						int y1 = line.spoint.y;
						int x2 = line.epoint.x;
						int y2 = line.epoint.y;
						AlgoVisHelper.drawLine(g2d, x1, y1, x2, y2);
						
						
						
						
					}
				}
			}
			
			if (text!=null) {
				for (int i = 0; i < text.size(); i++) {
//					AlgoVisHelper.setColor(g2d,Color.RED);

					AlgoVisHelper.drawString(g2d, text.get(i), textpoint.get(i).x, textpoint.get(i).y);
					
					
				}
//				System.out.println(text.size());

			}
			
			
			

			AlgoVisHelper.setStrokeWidth(g2d, 5);
			AlgoVisHelper.setColor(g2d,Color.RED);

			AlgoVisHelper.drawString(g2d, AlgoVisualizer.midTime+"", 90, 50);


			
		}
		
		
		@Override
		public Dimension getPreferredSize() {
			
			return new Dimension(canvasWidth, canvasHeight);
		}
		
	}


	
	
}
